package codetop.greedy;

import java.util.Arrays;
import java.util.Comparator;

public class Code1288 {
    public int removeCoveredIntervals(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0]==o2[0])return o2[1]-o1[1];
                return o1[0]-o2[0];
            }
        });
        int start=intervals[0][0];
        int end=intervals[0][1];
        int delete=0;
        for (int i = 1; i < intervals.length; i++) {
            int start1=intervals[i][0];
            int end1=intervals[i][1];
            if (start1>=start&&end1<=end){
                delete++;
            }else {
                start=start1;
                end=end1;
            }
        }
        return intervals.length-delete;
    }
}
